package com.example.service.impl;

import com.example.common.Result;
import com.example.service.MemberDiscountService;
import com.example.vo.DiscountInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 会员折扣服务实现类
 */
@Slf4j
@Service
public class MemberDiscountServiceImpl implements MemberDiscountService {
    
    /**
     * 会员折扣率配置
     * VIP1（一级会员）：8折 = 0.8
     * VIP2（二级会员）：9折 = 0.9  
     * VIP3（三级会员）：95折 = 0.95
     * NORMAL（普通用户）：无折扣 = 1.0
     */
    private static final BigDecimal VIP1_DISCOUNT_RATE = new BigDecimal("0.8");
    private static final BigDecimal VIP2_DISCOUNT_RATE = new BigDecimal("0.9");
    private static final BigDecimal VIP3_DISCOUNT_RATE = new BigDecimal("0.95");
    private static final BigDecimal NORMAL_DISCOUNT_RATE = new BigDecimal("1.0");
    
    @Override
    public Result<BigDecimal> calculateDiscountPrice(BigDecimal originalPrice, String memberLevel) {
        try {
            if (originalPrice == null || originalPrice.compareTo(BigDecimal.ZERO) <= 0) {
                return Result.error("原价不能为空或小于等于0");
            }
            
            BigDecimal discountRate = getDiscountRate(memberLevel);
            BigDecimal discountPrice = originalPrice.multiply(discountRate).setScale(2, RoundingMode.HALF_UP);
            
            log.debug("价格折扣计算 - 原价: {}, 会员等级: {}, 折扣率: {}, 折扣后价格: {}", 
                originalPrice, memberLevel, discountRate, discountPrice);
            
            return Result.success(discountPrice);
        } catch (Exception e) {
            log.error("计算折扣价格失败", e);
            return Result.error("计算折扣价格失败");
        }
    }
    
    @Override
    public Result<BigDecimal> getMemberDiscountRate(String memberLevel) {
        try {
            BigDecimal discountRate = getDiscountRate(memberLevel);
            return Result.success(discountRate);
        } catch (Exception e) {
            log.error("获取会员折扣率失败", e);
            return Result.error("获取会员折扣率失败");
        }
    }
    
    @Override
    public Result<DiscountInfoVO> getMemberDiscountInfo(BigDecimal originalPrice, String memberLevel) {
        try {
            if (originalPrice == null || originalPrice.compareTo(BigDecimal.ZERO) <= 0) {
                return Result.error("原价不能为空或小于等于0");
            }
            
            BigDecimal discountRate = getDiscountRate(memberLevel);
            BigDecimal discountPrice = originalPrice.multiply(discountRate).setScale(2, RoundingMode.HALF_UP);
            BigDecimal savedAmount = originalPrice.subtract(discountPrice).setScale(2, RoundingMode.HALF_UP);
            String memberLevelName = getMemberLevelName(memberLevel);
            
            DiscountInfoVO discountInfo = new DiscountInfoVO();
            discountInfo.setOriginalPrice(originalPrice);
            discountInfo.setDiscountRate(discountRate);
            discountInfo.setDiscountPrice(discountPrice);
            discountInfo.setSavedAmount(savedAmount);
            discountInfo.setMemberLevel(memberLevel);
            discountInfo.setMemberLevelName(memberLevelName);
            
            log.debug("会员折扣信息 - 原价: {}, 会员等级: {}, 折扣率: {}, 折扣价: {}, 节省: {}", 
                originalPrice, memberLevel, discountRate, discountPrice, savedAmount);
            
            return Result.success(discountInfo);
        } catch (Exception e) {
            log.error("获取会员折扣信息失败", e);
            return Result.error("获取会员折扣信息失败");
        }
    }
    
    @Override
    public boolean isVipMember(String memberLevel) {
        return memberLevel != null && (
            "VIP1".equals(memberLevel) || 
            "VIP2".equals(memberLevel) || 
            "VIP3".equals(memberLevel)
        );
    }
    
    @Override
    public String getMemberLevelName(String memberLevel) {
        if (memberLevel == null) {
            return "普通用户";
        }
        
        switch (memberLevel) {
            case "VIP1":
                return "一级会员";
            case "VIP2":
                return "二级会员";
            case "VIP3":
                return "三级会员";
            case "NORMAL":
            default:
                return "普通用户";
        }
    }
    
    /**
     * 根据会员等级获取折扣率
     */
    private BigDecimal getDiscountRate(String memberLevel) {
        if (memberLevel == null) {
            return NORMAL_DISCOUNT_RATE;
        }
        
        switch (memberLevel) {
            case "VIP1":
                return VIP1_DISCOUNT_RATE; // 一级会员8折
            case "VIP2":
                return VIP2_DISCOUNT_RATE; // 二级会员9折
            case "VIP3":
                return VIP3_DISCOUNT_RATE; // 三级会员95折
            case "NORMAL":
            default:
                return NORMAL_DISCOUNT_RATE; // 普通用户无折扣
        }
    }
} 